package com.yodawnla.attackTitan2;

import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.anddev.andengine.entity.modifier.MoveModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.util.MathUtils;
import org.anddev.andengine.util.modifier.IModifier;
import org.anddev.andengine.util.modifier.ease.EaseExponentialOut;

import com.yodawnla.attackTitan2.scene.GameScene;
import com.yodawnla.attackTitan2.system.SE;
import com.yodawnla.lib.util.tool.YoTimer;

public class Coin extends Sprite
{
	GameScene mScene;
	boolean mIsTake = false;
	IEntityModifierListener mCoinListener;
	YoTimer mUpdateTimer;
	
	public Coin(float pX, float pY, TextureRegion pTextureRegion, GameScene scene)//YoScene scene, 
	{
		super(pX, pY, pTextureRegion);
		setVisible(false);
		mScene = (GameScene)scene;		
		setZIndex(2);
		
		mUpdateTimer = new YoTimer(0.1f)
		{
			@Override
			public void onTimePassed()
			{
				mScene.getBaseActivity().runOnUpdateThread(new Runnable()
				{
					public void run()
					{
						if(!Coin.this.isVisible() || mIsTake)
						{
							pause();
							return;
						}
						
						if(Coin.this.collidesWith(mScene.getCollide()))
							take();
					}
				});
			}
		};
		mUpdateTimer.start();
		
		mCoinListener = new IEntityModifierListener()
		{
			public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem)
			{
			}

			public void onModifierFinished(IModifier<IEntity> pModifier, final IEntity pItem)
			{
					pItem.setAlpha(1);
					pItem.setVisible(false);					
//					if(mIsTake)
//						mScene.addScore(100);
					setIsTake(false);
					//mScene.playSound("SeCoin2");
			}
		};
	}

	public void setIsTake(boolean isTake)
	{
		mIsTake = isTake;
	}
	
	public boolean getIsTake()
	{
		return mIsTake;
	}
	
	public void take()
	{
		if(mIsTake)
			return;
		
		setIsTake(true);
		clearEntityModifiers();
//		registerEntityModifier(new MoveYModifier(0.2f, getY(), getY() - 100));
//		registerEntityModifier(new AlphaModifier(0.2f, 1, 0, mCoinListener));
//		mScene.addScoreAndHit(100, getX(), getY());
		registerEntityModifier(new MoveModifier(0.6f, getX(), getX(), getY(), getY() - 150, mCoinListener, EaseExponentialOut.getInstance()));
		registerEntityModifier(new AlphaModifier(0.6f, 1, 0));
		//mScene.addHit(getX(), getY());
		mScene.addGold((int)(Player.getInstance().getGoldGain() * getScaleX()));// * 10));
//		mScene.addGold((int)(15 * getScaleX()));
		SoundMgr.getInstance();
		if(mScene.getIsCanPlaySound())
		{
			mScene.setIsCanPlaySoundOff();
			mScene.playSound(SE.Coin);
		}
	}
	
	public void showLevelCoin()
	{
		mUpdateTimer.resume();
		setVisible(true);
		clearEntityModifiers();
		registerEntityModifier(new MoveModifier(
			1f, 240, MathUtils.random(0, 450), 800, MathUtils.random(50, 500), EaseExponentialOut.getInstance()
		));
		registerEntityModifier(new SequenceEntityModifier(mCoinListener,
			new AlphaModifier(0.2f, 0.1f, 1), new AlphaModifier(10f, 1, 1), new AlphaModifier(1f, 1, 0)
		));
	}
	
	public void show(float pX, float pY)
	{
		mUpdateTimer.resume();
		setVisible(true);
		setScale(MathUtils.random(0.5f, 1.5f));
		clearEntityModifiers();
//		registerEntityModifier(new LoopEntityModifier(new RotationModifier(1, 0, 360)));
		float pX2 = MathUtils.random(pX - 90, pX + 150);
		if(pX2 < 0)
			pX2 = 0;
		else if(pX2 > 430)
			pX2 = 430;
		
		float pY2 = MathUtils.random(pY - 90, pY + 110);
		if(pY2 < 50)
			pY2 = 50;
		else if(pY2 > 500)
			pY2 = 500;
		
		registerEntityModifier(new MoveModifier(
			1f, pX, pX2, pY, pY2, EaseExponentialOut.getInstance()
		));
		registerEntityModifier(new SequenceEntityModifier(mCoinListener,
			new AlphaModifier(0.2f, 0.1f, 1), new AlphaModifier(10f, 1, 1), new AlphaModifier(1f, 1, 0)
		));
	}
	
	public void resetCoin()
	{
		clearEntityModifiers();
		mIsTake = false;
		setVisible(false);
	}
}